package com.minimalist.basic.controller.basic;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.minimalist.basic.service.ConfigService;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.config.ConfigQueryVO;
import com.minimalist.common.entity.basic.vo.config.ConfigVO;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "参数配置管理")
@RequestMapping("/basic/config")
public class ConfigController{

    @Autowired
    private ConfigService configService;

    @PostMapping("/addConfig")
    @SaCheckPermission("basic:config:add")
    @Operation(summary = "添加参数")
    public R<?> addConfig(@RequestBody @Validated(Add.class) ConfigVO configVO){
        configService.addConfig(configVO);
        return R.ok();
    }

    @DeleteMapping("/deleteConfigByConfigId")
    @SaCheckPermission("basic:config:delete")
    @Operation(summary = "删除参数 -> 根据参数ID删除")
    public R<?> deleteConfigByConfigId(
            @RequestParam("configId") @NotNull(message = "参数ID不能为空")
            @Parameter(name = "configId", required = true, description = "参数ID")
            Long configId){
        configService.deleteConfigByConfigId(configId);
        return R.ok();
    }

    @PutMapping("/updateConfigByConfigId")
    @SaCheckPermission("basic:config:update")
    @Operation(summary = "修改参数")
    public R<?> updateConfigByConfigId(@RequestBody @Validated(Update.class) ConfigVO configVO){
        configService.updateConfigByConfigId(configVO);
        return R.ok();
    }

    @GetMapping("/getPageConfigList")
    @SaCheckPermission("basic:config:get")
    @Operation(summary = "查询参数配置列表(分页)")
    public R<PageResp<ConfigVO>> getPageConfigList(ConfigQueryVO queryVO){
        return R.ok(configService.getPageConfigList(queryVO));
    }

    @GetMapping("/getConfigByConfigId/{configId}")
    @SaCheckPermission("basic:config:get")
    @Operation(summary = "根据参数ID查询参数")
    public R<ConfigVO> getConfigByConfigId(
            @PathVariable(value = "configId") @NotNull(message = "参数ID不能为空")
            @Parameter(name = "configId", description = "参数ID", required = true)
            Long configId){
        return R.ok(configService.getConfigByConfigId(configId));
    }

}
